//
//  YZMyStockEditViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/30.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMyStockEditViewController.h"
#import "YZMAppStyle.h"
#import "YZMyStocksEditCell.h"
#import "YZMyStocksHeaderView.h"
#import "YZMSavedStock.h"
#import "YZMyStocksService.h"
#import <Masonry/Masonry.h>

NSString *const sYZMyStockEditCellID = @"YZMyStockEditCellID";
NSString *const sYZMyStockEditHeaderID = @"YZMyStockEditHeaderID";

@interface YZMyStockEditViewController()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) NSMutableSet *deleteSet;

@end

@implementation YZMyStockEditViewController

- (instancetype)init{
    
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"我的自选";
    }
    return self;
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    self.dataList = [NSMutableArray arrayWithArray:[YZMyStocksService sharedService].stocks];
    self.deleteSet = [NSMutableSet set];
    self.navigationItem.leftBarButtonItem = [YZMAppStyle leftBarItemWithTitle: @"完成" target: self action: @selector(leftBarItemPressed:)];
    self.navigationItem.rightBarButtonItem = [YZMAppStyle rightBarItemWithTitle: @"删除" target: self action: @selector(rightBarItemPressed:)];
    [self createObjects];
}

#pragma mark - operations

- (void)makeTopOfStock: (YZMSavedStock *)stock
{
    NSInteger row = [self.dataList indexOfObject: stock];
    if (row == 0) {
        return;
    }
    [[YZMyStocksService sharedService] topStockWithCode:stock.code];
    [self.tableView beginUpdates];
    [self.dataList removeObjectAtIndex: row];
    [self.dataList insertObject: stock atIndex: 0];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: [NSIndexPath indexPathForRow: row inSection:0]] withRowAnimation: UITableViewRowAnimationFade];
    [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: 0 inSection:0]] withRowAnimation: UITableViewRowAnimationTop];
    [self.tableView endUpdates];
}

#pragma mark -- event

- (void)leftBarItemPressed:(id)sender{
    
    [YZMCMInstance popViewController];
}

- (void)rightBarItemPressed:(id)sender{
    
    [[YZMyStocksService sharedService] removeSavedStocksWithCodeList:self.deleteSet];
    [self.dataList removeAllObjects];
    [self.dataList addObjectsFromArray:[YZMyStocksService sharedService].stocks];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 64;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 52;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    YZMyStocksHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:sYZMyStockEditHeaderID];
    headerView.style = YZMStockHeaderTypeForEdit;
    return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YZMyStocksEditCell *cell = [tableView dequeueReusableCellWithIdentifier: sYZMyStockEditCellID forIndexPath:indexPath];
    cell.record = self.dataList[indexPath.row];
    __weak typeof(&*self) safeSelf = self;
    cell.topAction = ^(id record){
        [safeSelf makeTopOfStock: record];
    };
    cell.selectAction = ^(BOOL addFlag, YZMSavedStock *record){
        
        if (addFlag) {
            
            [safeSelf.deleteSet addObject:record.code];
        }else{
            [safeSelf.deleteSet removeObject:record.code];
        }
    };
    YZMIndexCount indexCount;
    indexCount.index = indexPath.row;
    indexCount.count = self.dataList.count;
    cell.indexCount = indexCount;
    return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    [[YZMyStocksService sharedService] exchangeStockAtIndex:fromIndexPath.row toIndex:toIndexPath.row];
    [self.dataList exchangeObjectAtIndex: fromIndexPath.row withObjectAtIndex: toIndexPath.row];
}

#pragma mark -- ui

- (void)createObjects
{
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[YZMyStocksEditCell class] forCellReuseIdentifier:sYZMyStockEditCellID];
    [self.tableView registerClass:[YZMyStocksHeaderView class] forHeaderFooterViewReuseIdentifier:sYZMyStockEditHeaderID];
    [self.view addSubview: self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.editing = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

@end
